# -*- coding: utf-8 -*-
# Description:
# Created: tangyu 2023/5/8
from app.main.dao.sqlalchemy_dao import SQLAlchemyDao
from app.util.result import Result


# 数据库访问层，新加方法调用SQLAlchemyDao中的方法即可
class PerformancePredirtionModelDao():

    @staticmethod
    def select_model_data(input_list):
        """
        查询屈服强度模型的各种参数
        :return:
        """

        sql = """{}
        """
        sql = sql.format(input_list['sql'])
        # if input_list['group_var'] != '':
        #     sql = sql +'and sg_small_class like \'{}%\''.format(input_list['group_var'])
        # print(sql)
        try:
            data = SQLAlchemyDao.select_all_tg_RDS(sql)
            # print(data)
        except:
            return Result.error("{}报错".format(sql))
        return data


    def insert_model_root_nogroup(self,model_name,model_root,model_time,columns_str,upper_error,lower_error,reel_key,Y):
        sql = """
            insert into CPZLCF.MODELLOAD(MODEL_NAME,MODEL_LOAD,INSERT_TIME,MODEL_FEATURE,UPPER_ERROR,LOWER_ERROR,REEL_KEY,Y) 
            VALUES('{}','{}','{}','{}','{}','{}','{}','{}')
        """
        sql = sql.format(model_name,model_root,model_time,columns_str,upper_error,lower_error,reel_key,Y)
        # data = SQLAlchemyDao.execute(sql)
        return {"sql":sql,"params":{}}

    def insert_model_root_group(self,model_name,model_root,model_time,group_var,group_var_val,columns_str,upper_error,lower_error,reel_key,Y):
        sql = """
            insert into CPZLCF.MODELLOAD(MODEL_NAME,MODEL_LOAD,INSERT_TIME,GROUP_VAR,GROUP_VAR_VAL,MODEL_FEATURE,UPPER_ERROR,LOWER_ERROR,REEL_KEY,Y) 
            VALUES('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')
        """
        sql = sql.format(model_name,model_root,model_time,group_var,group_var_val,columns_str,upper_error,lower_error,reel_key,Y)
        # data = SQLAlchemyDao.execute(sql)
        return {"sql":sql,"params":{}}
    def select_model_name_group(self,model_name,group_var):
        sql = """
        select * from CPZLCF.MODELLOAD
        where MODEL_NAME = '{}' and MODEL_LOAD NOT LIKE '%no_group%'
        """
        sql = sql.format(model_name,group_var)
        data = SQLAlchemyDao.select_all(sql)
        return data

    def select_model_name_nogroup(self,model_name):
        sql = """
        select * from CPZLCF.MODELLOAD
        where MODEL_NAME = '{}' and MODEL_LOAD LIKE '%no_group%'
        """
        sql = sql.format(model_name)
        data = SQLAlchemyDao.select_all(sql)
        return data

    def update_model_group(self,model_name,model_path,model_time,group_var,group_var_val,columns_str,upper_error,lower_error,reel_key,Y):
        sql = """
        UPDATE CPZLCF.MODELLOAD
         SET MODEL_NAME='{}',
         MODEL_LOAD='{}',
         INSERT_TIME='{}',
         MODEL_FEATURE = '{}',
         UPPER_ERROR='{}',
         LOWER_ERROR='{}',
         REEL_KEY = '{}',
         Y = '{}'
         WHERE MODEL_NAME like '{}' and MODEL_LOAD NOT LIKE '%no_group%' and GROUP_VAR = '{}' and GROUP_VAR_VAL = '{}'
        """
        sql = sql.format(model_name,model_path,model_time,columns_str,upper_error,lower_error,reel_key,Y,model_name,group_var,group_var_val)
        # data = SQLAlchemyDao.execute(sql)
        return {"sql":sql,"params":{}}

    def update_model_nogroup(self,model_name,model_path,model_time,columns_str,upper_error,lower_error,reel_key,Y):
        sql = """
        UPDATE CPZLCF.MODELLOAD
         SET MODEL_NAME='{}',
         MODEL_LOAD='{}',
         INSERT_TIME='{}',
         MODEL_FEATURE = '{}',
         UPPER_ERROR='{}',
         LOWER_ERROR='{}',
         REEL_KEY = '{}',
         Y = '{}'
         WHERE MODEL_NAME like '{}' and MODEL_LOAD LIKE '%no_group%'
        """
        sql = sql.format(model_name,model_path,model_time,columns_str,upper_error,lower_error,reel_key,Y,model_name)
        # data = SQLAlchemyDao.execute(sql)
        return {"sql":sql,"params":{}}

    def update_data(self,sql_list):
        SQLAlchemyDao.execute_many(sql_list)